from flask import Flask, render_template, redirect, request, session
import pymysql
from datetime import timedelta
import os
app = Flask(__name__, static_url_path="")
config = {
    "host": "yang-sql.mysql.rds.aliyuncs.com",
    "user": "yang",
    "password": "Yy123456",
    "db": "mydata",
    "port": 13306,
    "charset": "utf8"
}
# 配置session
app.config["SECRET_KEY"] = os.urandom(24)
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(hours=48)

@app.route("/", methods=["POST", "GET"])
def index():
    if request.method == "GET":
        return render_template("login.html")
    elif request.method == "POST":
        username = request.form.get("username")
        password = request.form.get("password")
        connect = pymysql.connect(**config)
        cursor = connect.cursor()
        result = cursor.execute("select * from user where u_username=%s and u_password=%s", (username, password))
        print(result)
        if result is not None:
            session["login"] = "true"
            return render_template("index.html")
        else:
            return redirect("/")

@app.route("/welcome")
def welcome():
    return render_template("welcome.html")




if __name__ == '__main__':
    app.run(host="0.0.0.0", port=7776, debug=True)